﻿using MyNeighbourhood.Enums;
using MyNeighbourhood.Filters;
using MyNeighbourhood.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Web;
using Mvc.Mailer;
using System.Web.Mvc;
using MyNeighbourhood.Mailers;
using MyNeighbourhood.Managers;

namespace MyNeighbourhood.Controllers
{
    public class HomeController : BaseController
    {
        public IUserMailer mailer = new UserMailer();

        private static List<ContactModel> contacts = new List<ContactModel>() {
            new ContactModel { Title = "Web site owner", Email = "didi.zlatkova93@gmail.com"},
            new ContactModel { Title = "Web site administrator", Email = "dzlatkova@melontech.com"},
            new ContactModel { Title = "Web site manager", Email = "manager@socialhood.com"},
            new ContactModel { Title = "Web site editor", Email = "dimitrina.zlatkova@occupie.openfmi.net"},
        };

        [HttpPost]
        public void SetSplashCookie(bool checkboxValue)
        {
            HttpCookie cookie = new HttpCookie("skip-splash-screen");
            cookie.Value = checkboxValue ? "yes" : "no";
            HttpContext.Response.SetCookie(cookie);
        }

        public ActionResult Splash()
        {
            if (HttpContext.Request.Cookies["skip-splash-screen"] != null)
            {
                HttpCookie cookie = HttpContext.Request.Cookies.Get("skip-splash-screen");
                if (cookie.Value == "yes")
                {
                    return RedirectToAction("Index");
                }
            }

            return View();
        }

        [InternalExternalAddress]
        public ActionResult Index()
        {
            var model = this.GetAllEventsModel();
            return View("ExternalIndex", model);
        }

        public ActionResult InternalIndex()
        {
            var model = this.GetAllEventsModel();
            return View("ExternalIndex", model);
        }

        [HttpGet]
        public ActionResult Feedback()
        {
            ViewBag.AllTypes = Enum.GetValues(typeof(FeedbackType));

            FeedbackModel model = new FeedbackModel();
            model.Receivers = new List<ReceiverCheck>();
            foreach (var contact in contacts)
            {
                model.Receivers.Add(new ReceiverCheck { Contact = contact });
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult Feedback(FeedbackModel model)
        {
            if (ModelState.IsValid)
            {
                mailer.UserFeedback(model).SendAsync();
                return RedirectToAction("Index");
            }

            return View(model);
        }

        public ActionResult Back(string returnUrl)
        {
            return Redirect(returnUrl);
        }

        public ActionResult About()
        {
            string model = null;
            if (this.Request.UrlReferrer == null)
            {
                model = this.Url.Action("Index");
            }
            else
            {
                model = this.Request.UrlReferrer.ToString();
            }

            return View((object)model);
        }

        public ActionResult Contact()
        {
            return View();
        }
    }
}
